#!/bin/bash
#
# Q's Doom Launcher v0.1
#
# A simple WAD & mod selector script for launching GZDoom via rofi/dmenu
#
# IWADs should all be stored in 1 directory, and all mods in another. 


#----------------
# CONFIG OPTIONS
#----------------
#
# This section holds variables to be edited for each user install

# Directory containing IWAD files
IWAD_DIR="$HOME/Games/Doom/"
## Ensure trailing slashes for the directory paths

# Directory containing mod files (*.wad/*.pk3 only supported at present)
MOD_DIR="$HOME/Games/Doom/Mods/"
## Ensure trailing slashes for the directory paths

# Display option for separator
SEPERATOR="-----"

# Menu system to use - valid options are "rofi" or "dmenu" - defaults to dmenu
MENU_SYSTEM="rofi"

# Set a prefix command to run before launching, for example optirun for optimus setups
PREFIX_COMMAND=""



#-----------------
# Set menu system
#-----------------
#
# This section will setup the menu system to use (either rofi or dmenu)

# Allow this script to define & use aliases
shopt -s expand_aliases

# Create an alias "DisplayMenu" to match format from chosen menu system
if [ $MENU_SYSTEM = rofi ]; then
    echo Using rofi
    alias DisplayMenu="rofi -dmenu"
else
    echo Using dmenu
    alias DisplayMenu="dmenu -l 25"
fi



#-------------
# Select IWAD
#-------------
#
# Selection process for base WAD for the play through

# Generate list of all IWAD files
IWADLIST=$(ls $IWAD_DIR | grep -i 'wad$')

# Select IWAD from list
BASEWAD=$(printf '%s\n' "$IWADLIST" | DisplayMenu -p "Base IWAD")



#-------------
# Select Mods
#-------------
#
# Selection process for mod(s) to be used

# Generate list of PK3/WAD Mod files
MODLIST=$(ls $MOD_DIR | grep -i -e 'wad$' -e 'pk3$')

# Variable FINISHED_SELECTION tracks when user has selected "done" to break the loop
FINISHED_SELECTION=1

# Variable containing list of all selected mods
RUNNINGMODS=""


while [ $FINISHED_SELECTION -gt 0 ]; do

    # Send modlist to dmenu
    SELECTEDMOD=$(printf '%s\n' "Done" "$SEPERATOR" "$MODLIST" "$SEPERATOR" "Reset" | DisplayMenu -p "Modifications")
    
    if [ $SELECTEDMOD = Done ]; then # Done

	# Break from loop
	FINISHED_SELECTION=0
	
    elif [ $SELECTEDMOD = Reset ]; then # Reset
	
	# Regenerate modlist, and clear running mods
	MODLIST=$(ls $MOD_DIR | grep -i -e 'wad$' -e 'pk3$')
	RUNNINGMODS=""
	
    elif [ $SELECTEDMOD = $SEPERATOR ]; then # Separator

	# Ensure loop will continue 
	FINISHED_SELECTION=1
	
    elif [ -z $SELECTEDMOD ]; then # No option returned by dmenu

	# Ensure loop continues, in case of dmenu issue (such as Esc key being pressed)
	FINISHED_SELECTION=1
	
    elif [ -n $SELECTEDMOD ]; then # Any non-zero output from dmenu, assumed to be a mod file selected

	# Add full path to mod to the running mods list
	RUNNINGMODS="$MOD_DIR$SELECTEDMOD $RUNNINGMODS"

        # Adjust the modlist to show a signifier against any chosen mods
	MODLIST=$(printf '%s\n' $MODLIST | sed s/$SELECTEDMOD/[+]$SELECTEDMOD/)

    else

	# Report issues if dmenu output is something unexpected
	echo Something has gone wrong...
	
    fi
done



#---------------
# Launch GZDoom
#---------------
#
# Launch doom with all selected details

$PREFIX_COMMAND gzdoom -iwad $IWAD_DIR$BASEWAD -file $RUNNINGMODS
